package anton.admin.lunch;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Joakim
 */

@ManagedBean(name="listLunch")
@SessionScoped

public class ListLunch implements Serializable {
    
    @Resource(name="jdbc/antonsDatasource")
        private DataSource ds;
    
    private LunchEntity lunchEntity;
    private LunchEntity lunch;
    
    private String lunchName;
    private String lunchDescription;
    private int lunchPrice;
    private String selectedValue;
    
    private int i;
    
    List<LunchEntity> list;
    
    //<editor-fold defaultstate="collapsed" desc="Set/get-functions">
        /**
     * @return the lunchName
     */
    public String getLunchName() {
        return lunchName;
    }

    /**
     * @param lunchName the lunchName to set
     */
    public void setLunchName(String lunchName) {
        this.lunchName = lunchName;
    }

    /**
     * @return the lunchDescription
     */
    public String getLunchDescription() {
        return lunchDescription;
    }

    /**
     * @param lunchDescription the lunchDescription to set
     */
    public void setLunchDescription(String lunchDescription) {
        this.lunchDescription = lunchDescription;
    }

    /**
     * @return the lunchPrice
     */
    public int getLunchPrice() {
        return lunchPrice;
    }

    /**
     * @param lunchPrice the lunchPrice to set
     */
    public void setLunchPrice(int lunchPrice) {
        this.lunchPrice = lunchPrice;
    }

    /**
     * @return the selectedValue
     */
    public String getSelectedValue() {
        return selectedValue;
    }

    /**
     * @param selectedValue the selectedValue to set
     */
    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }
    //</editor-fold>
    
    public List<LunchEntity> getLunches() throws SQLException {
        
        if(ds==null)
            throw new SQLException("Can't get data source");
        
        Connection con = ds.getConnection();
        
        if(con==null)
            throw new SQLException("can't get database connection");
        
        PreparedStatement ps = con.prepareStatement
            ("select lunch_name, lunch_description, lunch_price from Lunch");
        
        ResultSet result = ps.executeQuery();
        
        list = new ArrayList<>();
        
        while(result.next()){
            lunch = new LunchEntity();
            
            lunch.setLunchName(result.getString("lunch_name"));
            lunch.setLunchDescription(result.getString("lunch_description"));
            lunch.setLunchPrice(result.getInt("lunch_price"));
            
            list.add(lunch);
        }
        
        con.close();
        
        return list;
    }
    
    public String destroy(String LunchName) throws SQLException {
        
        String sql;
        String target = "/admin/listlunch?faces-redirect-true";
        
        try(Connection con = ds.getConnection()) {
            sql = "delete from Lunch where lunch_name = "+"'"+LunchName+"'";
            
            PreparedStatement ps;
            ps = con.prepareStatement(sql);
                
            i = ps.executeUpdate();
            
            for(int i=0; i<list.size(); i++)
            {
                lunchEntity = list.get(i);
                
                if(LunchName.equals(lunchEntity.getLunchName()))
                    list.remove(i);
            }
        }
        
        return target;
    }
    
    public String prepareUpdate(String lunchName, String lunchDescription, int lunchPrice) throws SQLException {
        
        String target = "/admin/lunch/updatelunch?faces-redirect=true";
        
        this.selectedValue = lunchName;
        this.lunchName = lunchName;
        this.lunchDescription = lunchDescription;
        this.lunchPrice = lunchPrice;
        
        return target;
    }
            
            
    public String update() throws SQLException{
        
        String target = "/admin/lunch/listlunch?faces-redirect=true";
        
        if(ds==null)
            throw new SQLException("Can't get data source");
        
        try(Connection con = ds.getConnection()) {
            
            if(con==null)
                throw new SQLException("Can't get database connection");
                
            String sql;
            
            sql = "update Lunch set lunch_name = ?, lunch_description = ?, lunch_price = ? where lunch_name = '"+selectedValue+"'";
            
            try(PreparedStatement ps = con.prepareStatement(sql)){
                
                ps.setString(1, lunchName);
                ps.setString(2, lunchDescription);
                ps.setInt(3, lunchPrice);
                
                ps.executeUpdate();
            }
        }        
        
        return target;        
    }
    
}
